﻿using QaaSimulation.QSim.Ships;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QaaSimulation.QSim
{
    class QSpaceShip : QSpaceObject
    {
        public QSpaceShip()
            : base()
        {
            QEngine engine = new QEngine();
            engine.ForwardAcceleration = 20;
            engine.BackwardAcceleration = -55;
            engine.MaximumSpeedBackward = -20;
            engine.MaximumSpeedForward = 150;
            engine.ActualSpeed = 10;
            m_Orientation.Position = new PointF(200, 200);
            m_Modules.Add(engine);

        }

        public override void Paint(Graphics g)
        {
            if (m_Model != null)
            {
                GraphicsState translateState = g.Save();

                g.TranslateTransform(m_Orientation.Position.X, m_Orientation.Position.Y);

                GraphicsState rotateState = g.Save();
                g.RotateTransform(m_Orientation.RotationAngle + 90);


                m_Model.Paint(g);
                g.Restore(rotateState);

                
                QEngine engine = ((QEngine)m_Modules[0]);
                float width = engine.MaximumSpeedForward - engine.MaximumSpeedBackward;
                float speed = this.m_Orientation.MovementSpeed;
                g.FillRectangle(Brushes.Lime, new RectangleF(-15, +12, 30 / width * speed, 10));
                g.DrawRectangle(Pens.White, new Rectangle(-15, +12, 30, 10));

                g.Restore(translateState);
                
            }
        }

        public override void Compute(float elapsedTime)
        {
            foreach (QModule module in m_Modules)
            {
                module.Compute(elapsedTime);
                if (module is QEngine)
                {
                    m_Orientation.MovementDirection = ((QEngine)module).CalculateNewMovement(m_Orientation.MovementDirection, m_Orientation.Direction, elapsedTime);
                    PointF newPosi = new PointF(m_Orientation.Position.X + m_Orientation.MovementDirection.X * elapsedTime, m_Orientation.Position.Y + m_Orientation.MovementDirection.Y * elapsedTime);

                    m_Orientation.Position = newPosi;
                }
            }
        }

        public static byte KeyCode(char key)
        {
            int value = (int)key;
            return (byte)(value & 0xFF);
        }

        public override void HandleInput(byte[] keys)
        {
            foreach (QModule module in m_Modules)
            {
                if (module is QEngine)
                {
                    if ((keys[KeyCode('W')] & 0x80) != 0)
                    {
                        ((QEngine)module).Acceleration = ((QEngine)module).ForwardAcceleration;
                    }
                    else if ((keys[KeyCode('S')] & 0x80) != 0)
                    {
                        ((QEngine)module).Acceleration = ((QEngine)module).BackwardAcceleration;
                    }
                    else
                    {
                        ((QEngine)module).Acceleration = 0;
                    }
                    if ((keys[KeyCode('A')] & 0x80) != 0)
                    {
                        m_Orientation.RotationAngle -= 10;
                    }
                    if ((keys[KeyCode('D')] & 0x80) != 0)
                    {
                        m_Orientation.RotationAngle += 10;
                    }

                }
            }


        }
    }
}
